package com.shujia.scala

object Demo8Class {
  def main(args: Array[String]): Unit = {

    val student: Student = new Student("001", "张三", 23, "1234567")

    println(student)

  }
}


class Person(mdn: String) {
  println("Person默认构造函数")

  val _mdn: String = mdn


  override def toString = s"Person(${_mdn})"
}


/**
  * extends Person(mdn)  继承父类，默认直接调用父类的构造函数
  *
  */

//默认构造函数
class Student(id: String, name: String, mdn: String) extends Person(mdn) {
  println("默认构造函数的函数体，最先执行")

  val _id: String = id
  val _name: String = name

  //如果变量定义没有赋值，需要使用占位符
  var _age: Int = _


  //构造函数重载
  def this(id: String, name: String, age: Int, mdn: String) {
    //在重载构造函数的第一行需要i先调用默认构造函数
    this(id, name, mdn)

    println("重载构造函数")

    //给成员变量赋值
    this._age = age
  }

  override def toString: String = s"student(${_id}\t${_name}\t${_age}\t${_mdn}"
}
